package org.jeecg.crawler.task;


import lombok.SneakyThrows;
import org.apache.commons.lang.time.DateFormatUtils;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.DateTimeUtil;
import org.jeecg.crawler.content.ContentNewsCrawler;
import org.jeecg.crawler.content.DetailContentNewsCrawler;
import org.jeecg.crawler.other.GettCrawler;
import org.jeecg.crawler.other.PostCrawler;
import org.jeecg.crawler.other.SearchNewsCrawler;
import org.jeecg.modules.crawlerpaper.entity.CrawlerInfo;
import org.jeecg.modules.crawlerpaper.service.ICrawlerInfoService;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class ContentScheduleTask {


    @Autowired
    private ICrawlerInfoService crawlerInfoService;

    @Autowired
    private ISysDictService sysDictService;
    //http://njrb.njdaily.cn/html /2020-11/26 /node_51.htm
    //http://njrb.njdaily.cn/html,0,/node_1.htm,0 ,conetntCss ,detailCss
    //参数：baseurl,dataurl,page,scoureName,articleTime;

    @SneakyThrows
    @Scheduled(cron = "0 10 9 * * ?")
    public void nanJingRiBaoCrawlerJob() {
        String parameter = "http://njrb.njdaily.cn/html,0,/node_1.htm,南京日报,0,li[class=BMedlist] a,div[class=pagenavlist] ul li a";

        String[] params = parameter.split(",");
        if (params[1].equals("0")) {
            params[1] = "/" + DateFormatUtils.format(new Date(), "yyyy-MM/dd");

        }
        if (params[4].equals("0")) {
            params[4] = DateTimeUtil.getTodayChar8En();
        }
        this.getCrewlerInfo(params[0], params[1], params[2], params[3], params[4], params[5], params[6]);


    }


    @SneakyThrows
    @Scheduled(cron = "0 11 9 * * ?")
    public void guangMingRiBaoCrawlerJob() {
        String parameter = "https://epaper.gmw.cn/gmrb/html,0,/nbs.D110000gmrb_01.htm,光明日报,0,div[id=pageList] ul li a ,div[id=titleList] ul li a";

        String[] params = parameter.split(",");
        if (params[1].equals("0")) {
            params[1] = "/" + DateFormatUtils.format(new Date(), "yyyy-MM/dd");

        }
        if (params[4].equals("0")) {
            params[4] = DateTimeUtil.getTodayChar8En();
        }
        this.getCrewlerInfo(params[0], params[1], params[2], params[3], params[4], params[5], params[6]);


    }


    @SneakyThrows
    @Scheduled(cron = "0 12 9 * * ?")
    public void zhongguoqingnianRiBaoCrawlerJob() {
        String parameter = "http://zqb.cyol.com/html,0,/nbs.D110000zgqnb_01.htm,中国青年报,0,div[id=pageList] ul li a ,div[id=titleList] ul li a";

        String[] params = parameter.split(",");
        if (params[1].equals("0")) {
            params[1] = "/" + DateFormatUtils.format(new Date(), "yyyy-MM/dd");

        }
        if (params[4].equals("0")) {
            params[4] = DateTimeUtil.getTodayChar8En();
        }
        this.getCrewlerInfo(params[0], params[1], params[2], params[3], params[4], params[5], params[6]);


    }

    @SneakyThrows
    @Scheduled(cron = "0 13 9 * * ?")
    public void zhongguofunvRiBaoCrawlerJob() {
        String parameter = "http://paper.cnwomen.com.cn/html,0,/nbs.D110000zgfnb_1.htm,中国妇女报,0,a[ id=pageLink] ,ul[class=list-unstyled]  li a";

        String[] params = parameter.split(",");
        if (params[1].equals("0")) {
            params[1] = "/" + DateFormatUtils.format(new Date(), "yyyy-MM/dd");

        }
        if (params[4].equals("0")) {
            params[4] = DateTimeUtil.getTodayChar8En();
        }
        this.getCrewlerInfo(params[0], params[1], params[2], params[3], params[4], params[5], params[6]);


    }

    @SneakyThrows
    @Scheduled(cron = "0 14 9 * * ?")
    public void jingjiRiBaoCrawlerJob() {
        String parameter = "http://paper.ce.cn/jjrb/html,0,/node_2.htm,经济日报,0,td [ class=default ][ align=left] a ,td[ class=default ][  valign=top] a";

        String[] params = parameter.split(",");
        if (params[1].equals("0")) {
            params[1] = "/" + DateFormatUtils.format(new Date(), "yyyy-MM/dd");

        }
        if (params[4].equals("0")) {
            params[4] = DateTimeUtil.getTodayChar8En();
        }
        this.getCrewlerInfo(params[0], params[1], params[2], params[3], params[4], params[5], params[6]);


    }

    @SneakyThrows
    @Scheduled(cron = "0 15 9 * * ?")
    public void kejiRiBaoCrawlerJob() {
        String parameter = "http://digitalpaper.stdaily.com/http_www.kjrb.com/kjrb/html,0,/node_2.htm,科技日报,0,div[class=bmname1]  a ,div[class=title] ul li a";

        String[] params = parameter.split(",");
        if (params[1].equals("0")) {
            params[1] = "/" + DateFormatUtils.format(new Date(), "yyyy-MM/dd");

        }
        if (params[4].equals("0")) {
            params[4] = DateTimeUtil.getTodayChar8En();
        }
        this.getCrewlerInfo(params[0], params[1], params[2], params[3], params[4], params[5], params[6]);


    }

    @SneakyThrows
    @Scheduled(cron = "0 16 9 * * ?")
    public void jiefangjunBaoCrawlerJob() {
        String parameter = "http://www.81.cn/jfjbmap/content,0,/node_2.htm,解放军报,0,ul[class = list-unstyled]  li a ,div[class =newslist-item current] ul li a";

        String[] params = parameter.split(",");
        if (params[1].equals("0")) {
            params[1] = "/" + DateFormatUtils.format(new Date(), "yyyy-MM/dd");

        }
        if (params[4].equals("0")) {
            params[4] = DateTimeUtil.getTodayChar8En();
        }
        this.getCrewlerInfo(params[0], params[1], params[2], params[3], params[4], params[5], params[6]);


    }


    @SneakyThrows
    @Scheduled(cron = "0 17 9 * * ?")
    public void jinlingwanBaoCrawlerJob() {
        String parameter = "http://jlwb.njnews.cn/html,0,/node_2.htm,金陵晚报,0,div[class=WH100 Left_bg01 iframeScroll]   ul li a ,div[class=ui-btn-text]  a";

        String[] params = parameter.split(",");
        if (params[1].equals("0")) {
            params[1] = "/" + DateFormatUtils.format(new Date(), "yyyy-MM/dd");

        }
        if (params[4].equals("0")) {
            params[4] = DateTimeUtil.getTodayChar8En();
        }
        this.getCrewlerInfo(params[0], params[1], params[2], params[3], params[4], params[5], params[6]);


    }

    @SneakyThrows
    @Scheduled(cron = "0 18 9 * * ?")
    public void nanjingriBaoCrawlerJob() {
        String parameter = "http://njrb.njdaily.cn/html,0,/node_1.htm,南京日报,0,li[class=BMedlist] a,div[class=pagenavlist] ul li a";

        String[] params = parameter.split(",");
        if (params[1].equals("0")) {
            params[1] = "/" + DateFormatUtils.format(new Date(), "yyyy-MM/dd");

        }
        if (params[4].equals("0")) {
            params[4] = DateTimeUtil.getTodayChar8En();
        }
        this.getCrewlerInfo(params[0], params[1], params[2], params[3], params[4], params[5], params[6]);


    }

    @Scheduled(cron = "0 19 9 * * ?")
    public void getJiangsuGongrenBaoCrewlerInfo() throws Exception {
        String date = DateTimeUtil.getTodayChar8();

        Map<String, String> urlMap = SearchNewsCrawler.getJiangsuGongrenBao("http://epaper.jsgrb.com/Media/jsgrb/" + date);

        try {
            ContentNewsCrawler.lock.lock();

            DetailContentNewsCrawler crawlerDetail = new DetailContentNewsCrawler("crawl", true, urlMap, "http://epaper.jsgrb.com", "江苏工人报", date, "div[class=nav1] ul li a");
            crawlerDetail.start(1);
            List<CrawlerInfo> crawlerInfoList = crawlerDetail.getCrawlerInfoList();
            if (!CollectionUtils.isEmpty(crawlerInfoList)) {
                List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
                if (!CollectionUtils.isEmpty(keywords)) {

                    Iterator<CrawlerInfo> iterator = crawlerInfoList.iterator();
                    while (iterator.hasNext()) {
                        CrawlerInfo next = iterator.next();
                        Boolean removeFlag = true;
                        for (DictModel keyword : keywords) {
                            if (next.getArticleName().contains(keyword.getValue()) || next.getArticleContent().toString().contains(keyword.getValue())) {
                                removeFlag = false;
                                break;
                            }
                        }
                        if (removeFlag) {
                            iterator.remove();
                        }

                    }
                }
                if (!CollectionUtils.isEmpty(crawlerInfoList)) {
                    crawlerInfoService.saveOrUpdateBatch(crawlerInfoList);

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ContentNewsCrawler.lock.unlock();


        }

    }


    /**
     * 工人日报
     * @throws Exception
     */
    @Scheduled(cron = "0 20 9 * * ?")
    public void getGongRenRiBao() throws Exception {


        try {

            List<CrawlerInfo> crawlerInfoList = GettCrawler.getGongRenRiBao();
            if (!CollectionUtils.isEmpty(crawlerInfoList)) {
                List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
                if (!CollectionUtils.isEmpty(keywords)) {

                    Iterator<CrawlerInfo> iterator = crawlerInfoList.iterator();
                    while (iterator.hasNext()) {
                        CrawlerInfo next = iterator.next();
                        Boolean removeFlag = true;
                        for (DictModel keyword : keywords) {
                            if (next.getArticleName().contains(keyword.getValue()) || next.getArticleContent().toString().contains(keyword.getValue())) {
                                removeFlag = false;
                                break;
                            }
                        }
                        if (removeFlag) {
                            iterator.remove();
                        }

                    }
                }
                if (!CollectionUtils.isEmpty(crawlerInfoList)) {
                    crawlerInfoService.saveOrUpdateBatch(crawlerInfoList);

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 中国纪检监察报
     * @throws Exception
     */

    @Scheduled(cron = "0 21 9 * * ?")
    public void getZhonguoJiJianBao() throws Exception {


        try {
            ContentNewsCrawler.lock.lock();

            List<CrawlerInfo> crawlerInfoList = PostCrawler.getZGJJJCBao();
            if (!CollectionUtils.isEmpty(crawlerInfoList)) {
                List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
                if (!CollectionUtils.isEmpty(keywords)) {

                    Iterator<CrawlerInfo> iterator = crawlerInfoList.iterator();
                    while (iterator.hasNext()) {
                        CrawlerInfo next = iterator.next();
                        Boolean removeFlag = true;
                        for (DictModel keyword : keywords) {
                            if (next.getArticleName().contains(keyword.getValue()) || next.getArticleContent().toString().contains(keyword.getValue())) {
                                removeFlag = false;
                                break;
                            }
                        }
                        if (removeFlag) {
                            iterator.remove();
                        }

                    }
                }
                if (!CollectionUtils.isEmpty(crawlerInfoList)) {
                    crawlerInfoService.saveOrUpdateBatch(crawlerInfoList);

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ContentNewsCrawler.lock.unlock();


        }

    }
    public void getCrewlerInfo(String baseUrl, String dateUrl, String page, String sourceName, String articleTime, String contentCss, String detailCss) throws Exception {
        try {
            ContentNewsCrawler.lock.lock();

            ContentNewsCrawler crawler = new ContentNewsCrawler("crawl", true, baseUrl + dateUrl, page, contentCss);
            crawler.start(1);
            Map<String, String> urlMap = crawler.getUrlMap();

            DetailContentNewsCrawler crawlerDetail = new DetailContentNewsCrawler("crawl", true, urlMap, baseUrl + dateUrl, sourceName, articleTime, detailCss);
            crawlerDetail.start(1);
            List<CrawlerInfo> crawlerInfoList = crawlerDetail.getCrawlerInfoList();
            if (!CollectionUtils.isEmpty(crawlerInfoList)) {
                List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
                if (!CollectionUtils.isEmpty(keywords)) {

                    Iterator<CrawlerInfo> iterator = crawlerInfoList.iterator();
                    while (iterator.hasNext()) {
                        CrawlerInfo next = iterator.next();
                        Boolean removeFlag = true;
                        for (DictModel keyword : keywords) {
                            if (next.getArticleName().contains(keyword.getValue()) || next.getArticleContent().toString().contains(keyword.getValue())) {
                                removeFlag = false;
                                break;
                            }
                        }
                        if (removeFlag) {
                            iterator.remove();
                        }

                    }
                }
                if (!CollectionUtils.isEmpty(crawlerInfoList)) {
                    crawlerInfoService.saveOrUpdateBatch(crawlerInfoList);

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ContentNewsCrawler.lock.unlock();


        }


    }
}
